﻿Public Class db
    Public Function CS(Filename As String) As String
        Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Filename & "; Persist Security Info=False;"
    End Function

    Public Function GetDataReader(CONN As OleDb.OleDbConnection, ByVal Q As String) As OleDb.OleDbDataReader
        Dim CMD As New OleDb.OleDbCommand
        Dim R As OleDb.OleDbDataReader
        Try
            If CONN.State = ConnectionState.Closed Then CONN.Open()
            Try
                CMD.Connection = CONN
                CMD.CommandText = Q

                R = CMD.ExecuteReader(CommandBehavior.CloseConnection)
                Return R
            Catch E As OleDb.OleDbException
                Console.WriteLine(E.Message)
                Return Nothing
            End Try
        Catch E2 As OleDb.OleDbException
            Console.WriteLine(E2.Message)
            Return Nothing
            Exit Function
        Catch E3 As Exception
            Console.WriteLine(E3.Message)
            Return Nothing
            Exit Function
        Finally
            Try
                CMD.Dispose()
            Catch E As Exception
                Console.WriteLine(E.Message)
            End Try
            CMD.Dispose()
        End Try
    End Function

    Public Sub SetData(CONN As OleDb.OleDbConnection, ByVal Q As String)
        Dim CMD As New OleDb.OleDbCommand
        CMD.Connection = CONN
        CMD.CommandText = Q
        Try
            If CONN.State = ConnectionState.Closed Then CONN.Open()
            CMD.ExecuteNonQuery()
        Catch E As OleDb.OleDbException
            If E.Message.ToLower.Contains("duplicate values") Then
                Throw New Exception("Duplicate entry detected.")
            Else
                Console.WriteLine(E.Message)
            End If
            Exit Sub
        Finally
            If CONN.State = ConnectionState.Open Then CONN.Close()
            CMD.Dispose()
            CMD = Nothing
        End Try
    End Sub
End Class
